<template>
  <div>
    <span>导出需求确认书</span>
  </div>
</template>

<script>
import Docxtemplater from 'docxtemplater'
import PizZip from 'pizzip'
// import JSZip from 'jszip'
import JSZipUtils from 'jszip-utils'
import { saveAs } from 'file-saver'
import axios from 'axios'
import XLSX from 'xlsx'
import { mapGetters } from 'vuex'
import Cookies from "js-cookie";

export default {
  name: 'requirementReport',
  data () {
    return {
      name: '',
      errName: '',
      apply_batch: 0,
    }
  },
  props: {
    // id: {
    //   type: String,
    //   default: ''
    // }
  },
  created () {
  },
  computed: {

  },
  components: {
    // XLSX
  },
  methods: {
    async word (id,apply_company_code) {
      try {
        const res = await this.$http('getRequirementWordData', {
          id: id,
          apply_company_code: apply_company_code
        })
        if (!res) {
          return false
        }
        let project = res.projectList
        let device = res.deviceList
        let apply_company_name = res.apply_company_name
        let service_amount_total = res.service_amount_total
        let audit_amount_total = res.audit_amount_total
        // let apply_company_name = '测试项目'
        // let service_amount_total = '155555'
        // let audit_amount_total = '6666'
        // let project=[
        //     {"index":'1',"project_name":'项目1',"service_amount":"15000.2","audit_amount":"800"},
        //     {"index":'1',"project_name":'项目2',"service_amount":"11000.2","audit_amount":"1800"},
        // ]
        // let device=[
        //     {
        //       "deviceItem":[
        //         {"index":'1',"device_name":'设备1',"service_content":"服务内容1","zb_param":"参数1","apply_amount":"100","audit_amount":"200","sh_msg":"审核意见1","yw_sj":"运维期限1","remarks":"备注1"},
        //         {"index":'2',"device_name":'设备2',"service_content":"服务内容2","zb_param":"参数2","apply_amount":"200","audit_amount":"201","sh_msg":"审核意见2","yw_sj":"运维期限2","remarks":"备注2"},
        //       ],
        //     "project_name":"项目1",
        //     "total_price":"11111.1",
        //     "total_price2":"2222.2"
        //     },
        //   {
        //     "deviceItem":[
        //       {"index":'3',"device_name":'设备3',"service_content":"服务内容3","zb_param":"参数3","apply_amount":"300","audit_amount":"202","sh_msg":"审核意见1","yw_sj":"运维期限1","remarks":"备注1"},
        //       {"index":'4',"device_name":'设备4',"service_content":"服务内容4","zb_param":"参数4","apply_amount":"400","audit_amount":"202","sh_msg":"审核意见2","yw_sj":"运维期限2","remarks":"备注2"},
        //     ],
        //     "project_name":"项目2",
        //     "total_price":"131.1",
        //     "total_price2":"232.2"
        //   }
        // ]
        let obj = {
          apply_company_name,
          project,
          service_amount_total,
          audit_amount_total,
          device
        }
        console.log("-------------------------------")
        console.log(obj)
        console.log("-------------------------------")
        this.makeWord(obj)
      } catch (err) {
        this.$tips.error(this.errName + '导出失败' + err.msg,)
      }
    },
    makeWord (item) {
      JSZipUtils.getBinaryContent('/static/doc/requirement_confirmation_sheet.docx', (error, content) => {
        // 抛出异常
        if (error) {
          throw error
        }
        let zip = new PizZip(content)
        // 创建并加载docxtemplater实例对象
        let doc = new Docxtemplater(zip, { linebreaks: true }, { paragraphLoop: true })
        // linebreaks 开启换行  paragraphLoop  清除多余间隔
        // 设置模板变量的值
        doc.setData(item)

        try {
          // 用模板变量的值替换所有模板变量
          doc.render()
        } catch (error) {
          // 抛出异常
          let e = {
            message: error.message,
            name: error.name,
            stack: error.stack,
            properties: error.properties
          }
          throw error
        }
        // 生成一个代表docxtemplater对象的zip文件（不是一个真实的文件，而是在内存中的表示）
        let out = doc.getZip().generate({
          type: 'blob',
          mimeType: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
        })
        // 将目标文件对象保存为目标类型的文件，并命名
        saveAs(out, '需求确认书' + '.docx')
      })
    },
  }
}
</script>

<style scoped>
</style>
